import 'package:flutter/material.dart';
import 'package:flutter_aapstroe/searchPage.dart';
import 'updatePage.dart';
import 'appPage.dart';
import 'toadyPage.dart';
import 'gamePage.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primaryColor: Colors.white
      ),
      home: HomePage(),
    );
  }
}
class HomePage extends StatefulWidget{
  @override
  _HomePageState createState() {
    return _HomePageState();
  }
}
class _HomePageState extends State<HomePage> with SingleTickerProviderStateMixin{
  TabController controller;
  //tab内容区域
  List<Widget> list=[
    todayPage(),
    GamePage(),   
    AppPage(),
    UpdatePage(),
    SearchPage(),
  ];
  int _currentPage;
  @override
  void initState() {
    super.initState();
    _currentPage=0;
    //组件初始化时初始化控制器并设置参数
    controller=TabController(vsync: this,length: list.length,);
  }
  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: list.length,
      child: Scaffold(
      body:TabBarView(
        children: list,
        physics: NeverScrollableScrollPhysics(),
      ),
      bottomNavigationBar: Container(
        decoration: BoxDecoration(
          border: Border(
            top: BorderSide(color: Colors.grey,width: 0.3)
          ),
          //颜色设置为白色，这样可以取消掉水波纹效果
          color: Colors.white,
          
        ),
        height: 90.0,
        padding: EdgeInsets.only(top: 10),
        alignment: Alignment.topCenter,
        child: TabBar(
          //配置tabbar参数
          labelStyle: TextStyle(
            fontSize: 12.0,
          ),
          indicatorColor: Colors.transparent,
          labelColor: Color.fromRGBO(0, 123, 251, 1),
          unselectedLabelColor: Color.fromRGBO(160, 159, 155, 1),
          tabs: <Widget>[
            //底部导航
            Tab(
              child: Text("Today",style: TextStyle(fontSize: 13),),
              icon: IconBar(
                src: "images/today.png",
                currentPage: _currentPage,
                index: 0,
              ),
            ),
            Tab(
              child: Text("游戏",style: TextStyle(fontFamily: "楷体"),),
              icon: IconBar(
                src: "images/game.png",
                currentPage: _currentPage,
                index: 1,
              ),
            ),
            Tab(
              child: Text("App",style: TextStyle(fontSize: 13),),
              icon: IconBar(
                src: "images/app.png",
                currentPage: _currentPage,
                index: 2,
              ),
            ),
            Tab(
              child: Text("更新"),
              icon: IconBar(
                src: "images/update.png",
                currentPage: _currentPage,
                index: 3,
              ),
            ),
            Tab(
              child: Text("搜索"),
              icon: IconBar(
                src: "images/search.png",
                currentPage: _currentPage,
                index: 4,
              ),
            )
          ],
          //点击事件更换tab显示区域
          onTap: (index){
            setState(() {
              _currentPage=index;
            });
          },
          ),
      ),
        ),
    );
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}

//底部导航栏每个导航icon
class IconBar extends StatelessWidget{
  int currentPage;//tab的当前页
  String src;//传入的图片
  int index;//当前页
  IconBar({Key key,this.index,this.src,this.currentPage}):super(key:key);
  @override
  Widget build(BuildContext context) { 
    return Image.asset(
      src,
      //如果当前页面为tab的当前页，则表示本页是激活状态
      color: currentPage==index?Color.fromRGBO(0, 123, 251, 1):Color.fromRGBO(160, 159, 155, 1),
      width: 20,);
  }
}